﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//using System.Core;


namespace ArchitectingInnovation.PRISMvvM.Core.Caching
{
    public class ClientCache
    {
        public ClientCache()
        {
            _strongCache = new Dictionary<object, object>();
            _weakCache = new Dictionary<object, WeakReference>();
        
        }
        private readonly IDictionary<object, object> _strongCache;

        private readonly IDictionary<object, WeakReference> _weakCache;

        public void Add<T>(T item, object key, bool pin)
        {
            var cacheItem = new CacheItem<T>(item);
            if (pin)
            {
                _strongCache[key] = cacheItem;
            }
            else
            {
                _weakCache[key] = new WeakReference(cacheItem);
            }
        }

        public T Get<T>(T key)
        {
           
            if(_strongCache.ContainsKey(key))
            {
                dynamic item = _strongCache[key];
                return (T) item.Item;
            }
            if(_weakCache.ContainsKey(key) && _weakCache[key].IsAlive)
            {
                dynamic item = _weakCache[key].Target;
                return (T) item.Item;
            }
            return default(T);
        }

        public void Remove(object key)
        {
            _strongCache.Remove(key);
            _weakCache.Remove(key);
        }



        public void ClearAllCache()
        {
            //sciske
            _strongCache.Clear();
            _weakCache.Clear();
        }

        public void ClearWeakCache()
        {
            _weakCache.Clear();
        }

        public void ClearStrongCache()
        {
            _strongCache.Clear();
        }
        /// <summary>
        /// Counts strong cache items
        /// </summary>
        public int CountStrongCacheItems
        {
            get
            {
                return _strongCache.Count;
             }
        }
        /// <summary>
        /// Counts weak cache items
        /// </summary>
        public int CountWeakCacheItems
        {
            get
            {
                return _weakCache.Count;
               
            }
        }
    }
}
